package com.thirteengroup.casemg.controller;


import com.thirteengroup.casemg.domain.Department;
import com.thirteengroup.casemg.domain.JasonResult;
import com.thirteengroup.casemg.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/department")
public class departmentCotroller {

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping(value = "/list")
    @ResponseBody
    public JasonResult showAll(){
        List<Department> list=new ArrayList<>();
        list=departmentService.queryAllDepartment();
        JasonResult jasonResult=new JasonResult("查询部门成功",true,list);
        return jasonResult;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JasonResult deleteById(long id){
        departmentService.deleteById(id);
        JasonResult jasonResult=new JasonResult("删除成功",true);
        return jasonResult;
    }

    @RequestMapping(value = "/input")
    public ModelAndView input(Long id){
        ModelAndView mv=new ModelAndView();
        if(id!=null){
            Department dept=departmentService.slectOne(id);

            mv.addObject("dept",dept);

        }
        mv.setViewName("department/input");
        return mv;
    }

    @RequestMapping(value = "/upOrin")
    @ResponseBody
    public JasonResult upOrin(Department dept){

        String msg="";
        if(dept.getId()!=null){
            departmentService.updateDept(dept);
            msg="更新部门成功";
        }else {
            departmentService.insertDept(dept);
            msg="添加部门成功";
        }
        JasonResult jasonResult=new JasonResult(msg,true);
        return jasonResult;
    }
}
